﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GIMS.Def.Operators;

namespace GIMS.Def
{
    public static class OperatorFactory
    {
        private static List<Operator> availableOperators;

        static OperatorFactory()
        {
            availableOperators=GetOperators();
        }

        private static List<Operator> GetOperators()
        {
            List<Operator> result=new List<Operator>();
            Assembly a = Assembly.GetExecutingAssembly();
            Type[] types = a.GetTypes();
            Type operatorType = typeof(Operator);

            IEnumerable<Type> r = types.Where(x => x.IsSubclassOf(operatorType));

            foreach (var type in r)
            {
                result.Add((Operator)Activator.CreateInstance(type));
            }
            return result;
        } 

        public static Operator GetOperator(string operatorName)
        {
            Operator op = availableOperators.Where(x => x.Name.ToLower() == operatorName.ToLower()).First();
            Type opType=op.GetType();
            return (Operator)Activator.CreateInstance(opType);
        }

        public static IEnumerable<Operator> AvailableOperators
        {
            get
            {
                return availableOperators;
            }
        }
    }
}
